<template>
  <div class="page">
    <div class="searchline" style="margin: 20px 0">
      <el-input
        placeholder="职位名称"
        v-model="search.name"
        size="medium"
        style="width: 200px; margin-left: 12px"
      ></el-input>
      <el-select placeholder="状态" size="medium" v-model="search.status" style="width: 200px; margin-left: 12px">
        <el-option label="待审核" :value="0"> </el-option>
        <el-option label="上架" :value="1"> </el-option
        ><el-option label="下架" :value="2"> </el-option>
        <el-option label="强制下架" :value="3"> </el-option>

      </el-select>
      <el-button style="margin-left: 12px" size="medium" type="primary"
      @click="searchFromList(search)"
        >筛选</el-button
      >
      <el-button size="medium" type="info" @click="reSet()">重置</el-button>
    </div>
    <el-table
      header-row-class-name="tableHeader"
      :data="tableData"
      border
      style="width: 98%; margin-left: 12px"
    >
      <el-table-column prop="name" label="职位" width="150px" > </el-table-column>
      <el-table-column prop="count" label="人数" width="80px"> </el-table-column>
      <el-table-column prop="salary" label="资薪" width="80px"> </el-table-column>
      <el-table-column prop="introduction" label="简介"> </el-table-column>
      <el-table-column prop="status" label="投递状态" width="80px">
        <template slot-scope="scope">
          <span v-if="scope.row.status == 0" style="color: red">待审批</span>
          <span v-if="scope.row.status == 1" style="color: rgb(65, 151, 46)"
            >上架</span
          >
          <span v-if="scope.row.status == 2" style="color: grey">下架</span>
          <span v-if="scope.row.status == 3">强制下架</span>
        </template>
      </el-table-column>
      <el-table-column prop="created_date" label="创建日期" width="150px">
      </el-table-column>
      <!-- <el-table-column prop="updated_date" label="更新日期" width="150px">
      </el-table-column> -->
      <el-table-column prop="empty" label="操作" width="150px">
        <template slot-scope="scope">
          <span v-if="scope.row.status == 0">审批通过后上架</span>
          <el-button v-if="scope.row.status == 2" size="mini" type="primary" @click="setJobStatus(scope.row.id,1)"
            >上架</el-button
          >
          <el-button v-if="scope.row.status == 1" size="mini" type="info" @click="setJobStatus(scope.row.id,2)"
            >下架</el-button
          >
          <span v-if="scope.row.status == 3">联系管理员恢复</span>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { allJob,setJob } from "@/api/job";
import { localData } from "@/util/local";
import { dateFormat } from "@/util/common";

export default {
  data() {
    return {
      initTableData: [],
      tableData: [],
      userInfo: {},
      search: {
        name: "",
        status:""
      },
    };
  },
  created() {
    this.userInfo = localData("get", "userinfo");
  },
  mounted() {
    this.getAllJob();
  },
  methods: {
    getAllJob() {
      if(!this.userInfo.company_id){
        this.$message({
            message: "请先完善企业信息",
            type: "warning",
          });
          setTimeout(()=>{
            this.$router.push({ path: '/companyinfo' });
          },1000)
          return
      }
      const param = {
        company_id: this.userInfo.company_id,
      };
      allJob(param).then((res) => {
        this.initTableData = res.data.Jobs.map((item) => {
          item.created_date = dateFormat(
            "YYYY-mm-dd HH:MM",
            new Date(item.created_date)
          );
          item.updated_date = dateFormat(
            "YYYY-mm-dd HH:MM",
            new Date(item.updated_date)
          );
          return item;
        });
        this.tableData = this.initTableData;
        this.searchFromList(this.search)
      });
    },
    searchFromList(search) {
      const { name, status } = search;
      this.tableData = this.initTableData.filter((item) => {
        return (
          item.name.indexOf(name) !== -1 &&
          item.status.toString().indexOf(status.toString()) !== -1
        );
      });
    },
    reSet() {
      this.search = {
        name: "",
        status
      };
      this.tableData = this.initTableData;
    },
    setJobStatus(id,status){
      const param = {
        id:id,
        status:status
      }
      setJob(param).then(res =>{
        if(res.code === 200){
          this.$message({
              message: status===1 ? "上架成功" : "下架成功",
              type: "success",
          });
          this.getAllJob();
        }
      }).catch(err =>{
        this.$message({
            showClose: true,
            message: err.data,
            type: "warning",
          });
      })


    }
  },
};
</script>

<style>
.tableHeader {
  color: rgb(65, 151, 46) !important;
}
</style>